package com.cnxjs.shs4et.common;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {

    private static String signKey = "cnxjs"; // 密钥

    public static String generateJWT(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 60 * 60 * 1000 * 24 * 7))
                .sign(Algorithm.HMAC256(signKey));
    }

    public static Map<String, Object> vertifyJWT(String jwt) {
        return JWT.require(Algorithm.HMAC256(signKey))
                .build()
                .verify(jwt)
                .getClaim("claims")
                .asMap();
    }
}
